package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.TravelCondition;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.repository.TravelRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelRepository travelRepository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IDestinationService destinationService;


    @Override
    public void save(Travel travel) {
        travel.setId(null);
        travelRepository.save(travel);
    }

    @Override
    public void update(Travel travel) {
        travelRepository.save(travel);
    }

    @Override
    public void delete(String id) {
        travelRepository.deleteById(id);
    }

    @Override
    public Travel get(String id) {
        Travel travel = travelRepository.findById(id).orElse(null);
        if (travel !=null){
            travel.setAuthor(userInfoService.get(travel.getUserId()));
            travel.setDest(destinationService.get(travel.getDestId()));
            return travel;
        }
        return travel;
    }

    @Override
    public List<Travel> listAll() {
        return travelRepository.findAll();
    }

    @Override
    public Page<Travel> query(TravelQuery qo) {
        //创建查询对象
        Query query = new Query();

        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        TravelCondition day = qo.getDay();
        //出行消费
        TravelCondition perExpend = qo.getPerExpend();
        if (day!=null){
            query.addCriteria(Criteria.where("day").gte(day.getMin()).lte(day.getMax()));
        }
        if(perExpend != null){
            query.addCriteria(Criteria.where("perExpend").gte(perExpend.getMin()).lte(perExpend.getMax()));
        }

        Pageable pageable = null;
        //过滤
        if(qo.getOrderType() == 1){
            pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.DESC, "createTime");
        }if(qo.getOrderType() == 2){
            pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.DESC, "viewnum");
        }else {
            pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        }


        Page<Travel> page = DBHelper.query(template, Travel.class, query, qo.getPageable());

        //用户对象
        for (Travel travel:page){
            travel.setAuthor(userInfoService.get(travel.getUserId()));
        }

        return page;
    }

    @Override
    public String saveOrUpdate(Travel travel) {
        Destination destination = destinationService.get(travel.getDestId());
        //目的地名称
        travel.setDestName(destination.getName());
        //最后修改时间
        travel.setLastUpdateTime(new Date());
        //更新时间
        if (StringUtils.hasLength(travel.getId())){
            //获取travel1对象
            Travel travel1 = this.get(travel.getId());
            //设置点击数
            travel.setViewnum(travel1.getViewnum());
            //回复数
            travel.setReplynum(travel1.getReplynum());
            //收藏数
            travel.setFavornum(travel1.getFavornum());
            //点赞数
            travel.setSharenum(travel1.getSharenum());
            this.update(travel);
        }else {
            //创建时间
            travel.setCreateTime(new Date());
            this.save(travel);
        }

        return travel.getId();
    }

    @Override
    public void changeState(String id, int state) {
        Travel travel = this.get(id);
        if (travel == null){
            throw new LogicException("参数异常");
        }
        if (travel.getState()!= Travel.STATE_WAITING){
            throw new LogicException("不是待审核状态不允许审核");
        }

        if (state == Travel.STATE_RELEASE){
            //审核通过修改审核状态
            travel.setState(Travel.STATE_RELEASE);
            //修改发布时间
            travel.setCreateTime(new Date());
            //修改最后修改时间
            travel.setLastUpdateTime(new Date());
            this.update(travel);
        }else if (state == Travel.STATE_REJECT){
            travel.setState(Travel.STATE_REJECT);
            travel.setCreateTime(null);
            travel.setLastUpdateTime(new Date());
            this.update(travel);
        }

    }

    @Override
    public List<Travel> findByDestName(String keyword) {
        List<Travel> travels = travelRepository.findByDestName(keyword);
        for (Travel travel:travels){
            travel.setAuthor(userInfoService.get(travel.getUserId()));
            travel.setDest(destinationService.get(travel.getDestId()));
        }
        return travels;
    }


}
